<template>
	<xn-form-container
		:title="formData.id ? '编辑期货策略' : '增加期货策略'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="动态移动点数：" name="moveDot">
						<a-input v-model:value="formData.moveDot" placeholder="请输入动态移动点数" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="移动间隔天数：" name="moveDay">
						<a-input v-model:value="formData.moveDay" placeholder="请输入移动间隔天数" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="静默天数：" name="stopDay">
						<a-input v-model:value="formData.stopDay" placeholder="请输入静默天数" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="交易次数：" name="tradeTimes">
						<a-input v-model:value="formData.tradeTimes" placeholder="请输入交易次数" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="开始交易时间：" name="startTradeDate">
						<a-date-picker v-model:value="formData.startTradeDate" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择开始交易时间" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="结束交易时间：" name="endTradeDate">
						<a-date-picker v-model:value="formData.endTradeDate" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择结束交易时间" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="是否使用：" name="useFlag">
						<a-select v-model:value="formData.useFlag" placeholder="请选择是否使用" :options="useFlagOptions" />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="qhStrategyForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import qhStrategyApi from '@/api/biz/qhStrategyApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const useFlagOptions = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		useFlagOptions.value = tool.dictList('Y_N')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				qhStrategyApi
					.qhStrategySubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
					submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
